/*
	It started to be written by Roosevelt de Lima Sardinha, in April, 3rd, 2010.
	Roomenus is a javascript component for creating and managing menus in a web page.
*/
function menu(id, properties)
{
	this.id = id;
	this.text = properties.text ? properties.text : 'menu';
	this.action = properties.action ? properties.action : function(){};
	this.menuPrefix = properties.menuPrefix ? properties.menuPrefix : 'menu-';
	this.menuCSS = properties.menuCSS ? properties.menuCSS : 'default-main-menu';
	var display = properties.display ? properties.display : 'none';
	
	this.li = document.createElement('li');
	this.li.style.display = display;
	this.li.innerHTML = this.text;
	this.li.onclick = this.action;
	this.li.className = this.menuCSS;
	
	this.menus = new Array;
	
	var counter = 0;
	this.add = function(text, position)
	{
		// Adds a new menu.
		if (text == null)
		{
			text = this.menuPrefix + counter;
		}
		
		if(position == null)
		{
			position = this.menus.length;
		}
	
		if (this.li.getElementsByTagName('UL').length == 0)	
		{
			this.ul = document.createElement('ul');
			this.ul.className = this.menuCSS;
			this.li.appendChild(this.ul);
		}
			
		var myMenu = new menu(this.menuPrefix + counter, {
			text: text,
			menuCSS: this.menuCSS
		});
		if (position == this.menus.length)
		{
			this.ul.appendChild(myMenu.li);
			this.menus.push(myMenu);
		}			
		else
		{
			var nextObj = this.menus[position];	
			this.ul.insertBefore(myMenu.li, nextObj.li);
			this.menus.splice(position, 0, myMenu);
		}
		counter++;	

		myMenu.li.onmouseover = function(){
			var len = myMenu.menus.length;
			for(var i = 0; i < len; i++)
			{
				myMenu.menus[i].li.style.display = 'block';	
			}
		}
		
		myMenu.li.onmouseout = function(){
			var len = myMenu.menus.length;
			for(var i = 0; i < len; i++)
			{
				myMenu.menus[i].li.style.display = 'none';	
			}
		}		
		
		return myMenu.id;
	}
	
	this.remove = function(level, position, text){
		// Removes a menu.
		var removingMenu = this.menus[position]; 
		this.ul.removeChild(removingMenu.li);
		this.menus.splice(position, 1);
		removingMenu = null;
	}
	
	this.setAction = function(callback){
		this.li.onclick = callback;
	}
}
